package com.chuangshu.orange.controller;

import com.chuangshu.orange.entity.News;
import com.chuangshu.orange.result.Result;
import com.chuangshu.orange.result.ResultUtil;
import com.chuangshu.orange.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (News)表控制层
 *
 * @author makejava
 * @since 2021-03-21 17:30:05
 */
@RestController
@RequestMapping("news")
@Api(tags = {"新闻"})
public class NewsController {
    /**
     * 服务对象
     */
    @Resource
    private NewsService newsService;


    //查询所有新闻
    @ApiOperation(value = "查询所有的新闻")
    @GetMapping("selOnlyAll")
    public Result<Object> selOnlyAll(){
        List<News> news = this.newsService.queryOnlyAll();
        return ResultUtil.Success(news);
    }
    //查询一个新闻
    @ApiOperation(value = "查询一个新闻(分类1-5)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "新闻编号"),
    })
    @GetMapping("selOne")
    public Result<Object> selOne(Integer id){
        if(id == null){
            return ResultUtil.Error("404","参数不能为空",null);
        }
        News news = this.newsService.queryById(id);
        news.setLookedNum(news.getLookedNum()+1);
        News update = this.newsService.update(news);
        return ResultUtil.Success(update);
    }
    //给新闻点赞
    @ApiOperation(value = "给指定新闻点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "新闻编号"),
    })
    @PutMapping("upOne")
    public Result<Object> upOne(Integer id){
        //根据编号查询新闻
        News news = newsService.queryById(id);
        //将点赞数加1
        news.setThumb(news.getThumb()+1);
        //更新数据库
        News update = newsService.update(news);
        //返回
        return ResultUtil.Success(update);
    }
    //取消点赞
    @ApiOperation(value = "给指定新闻取消点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", dataType = "int", required = true, value = "新闻编号"),
    })
    @PutMapping("downOne")
    public Result<Object> downOne(Integer id){
        //根据编号查询新闻
        News news = newsService.queryById(id);
        //将点赞数加1
        news.setThumb(news.getThumb()-1);
        //更新数据库
        News update = newsService.update(news);
        //返回
        return ResultUtil.Success(update);
    }
}